﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using Safe_Driving;
using Safe_Driving.TokenUser;
using System.Windows;
using System.Data;
using System.Windows.Controls;
using System.Windows.Media;


namespace Safe_Driving
{
    public partial class MainWindow
    {
        /// <summary>
        /// Populate les users wordpress dans dataGrid_wp_user
        /// </summary>
       public void populate_usersWP() {
            SafeDriving WP_DB = SingletonTokenUser.GetInstance().WP_DB;

            IEnumerable<WpUsers> q = (from user in WP_DB.WpUsers
                                       select user);
            
            dataGrid_wp_user.ItemsSource = q;
        }

        /// <summary>
       /// Evt selection changed sur le dataGrid_wp_user
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       private void dataGrid_wp_user_SelectionChanged(object sender, SelectionChangedEventArgs e)
       {
           WpUsers user = (WpUsers)dataGrid_wp_user.SelectedItem;

           //Récupération du DC de WordPress
           SafeDriving WP_DB = SingletonTokenUser.GetInstance().WP_DB;

           txtEmail.Text = user.UserEmail;

           WpUserMeta user_meta = (from meta in WP_DB.WpUserMeta
                                   where meta.UserID == user.ID
                                           && meta.MetaKey == "first_name"
                                   select meta).SingleOrDefault();

           txtPrenom.Text = user_meta.MetaValue;

           user_meta = (from meta in WP_DB.WpUserMeta
                        where meta.UserID == user.ID
                                && meta.MetaKey == "last_name"
                        select meta).SingleOrDefault();

           txtNom.Text = user_meta.MetaValue;
           expanderCreerClient.IsExpanded = true;
       }

        
    
        private void AddUser()
        {
            // Mettre des else txtXXX.BorderBrush = UneTextBoxQuiChangePasDeCouleur.BorderBrush
            //Permettra de remettre la BorderBrush de la TextBox par défaut lorsque la validation a échouée, et qu'on l'a corrigée

            Verificator v = new Verificator();
            Security s = new Security();
            bool saisieOk = true;

            if (v.AssertName(txtNom.Text) == false)
            {
                saisieOk = false;
                txtNom.BorderBrush = Brushes.Red;
            }

            if (v.AssertName(txtPrenom.Text) == false)
            {
                saisieOk = false;
                txtPrenom.BorderBrush = Brushes.Red;
            }

            if (dtpDateNaissance.SelectedDate.HasValue == false)
            {
                saisieOk = false;
                dtpDateNaissance.BorderBrush = Brushes.Red;
            }

            if (txtAdresse.Text.Length == 0)
            {
                saisieOk = false;
                txtAdresse.BorderBrush = Brushes.Red;
            }

            if (v.AssertFrenchCP(txtVille.Text) == false)
            {
                saisieOk = false;
                txtCP.BorderBrush = Brushes.Red;
            }

            if (v.AssertCity(txtCP.Text) == false)
            {
                saisieOk = false;
                txtVille.BorderBrush = Brushes.Red;
            }

            if (!(txtTelFixe.Text == String.Empty) && (v.AssertTelFixeNumber(txtTelFixe.Text) == false))
            {
                saisieOk = false;
                txtTelFixe.BorderBrush = Brushes.Red;
            }

            if (!(txtTelPortable.Text == String.Empty) && (v.AssertTelPortableNumber(txtTelPortable.Text) == false))
            {
                saisieOk = false;
                txtTelPortable.BorderBrush = Brushes.Red;
            }

            if (!(txtEmail.Text == String.Empty) && (v.AssertEMail(txtEmail.Text) == false))
            {
                saisieOk = false;
                txtEmail.BorderBrush = Brushes.Red;
            }

            //Si la saisie est correcte, on insert le nouveau client.
            if (saisieOk == true)
            {
                SingletonTokenUser tok = SingletonTokenUser.GetInstance();
                AppSafeDriving dbApp = tok.DbApp;

                SFClient cl = new SFClient()
                {
                    IdaGenCe = 1,
                    StatUt = "Inscrit",
                    NoM = txtNom.Text,
                    PreNoM = txtPrenom.Text,
                    DateNAiSsaNcE = (DateTime) dtpDateNaissance.SelectedDate,
                    ADressE =  txtAdresse.Text,
                    CP = txtCP.Text,
                    VIllE = txtVille.Text,
                    TeLFixE = txtTelFixe.Text,
                    TeLPortable = txtTelPortable.Text,
                    Email = txtEmail.Text,
                    Login = txtNom.Text + txtPrenom.Text.Substring(0, 1),
                    Password = s.EncodePassword(s.GenereMotDePasse())
                };

                dbApp.SFClient.InsertOnSubmit(cl);
                dbApp.SubmitChanges();
                reset_client_field();

                 tok.Client_selected = cl;

                //Mise à jour du bandeau
                 RibbonLbl_ClientNom.Content = cl.NoM;
                 RibbonLbl_ClientPrenom.Content = cl.PreNoM;
                 RibbonLbl_ClientLogin.Content = cl.Login;

                Action_Client_Manager.IsEnabled = true;
                Client_Selection_Manager.IsEnabled = true;

                goToTab(Suivis_Client_TabItem);
                initClient();
            }
        }

        private void reset_client_field()
        {
            txtNom.Text = "";
            txtPrenom.Text = "";
            dtpDateNaissance.SelectedDate = DateTime.Now;
            txtAdresse.Text = "";
            txtCP.Text = "";
            txtVille.Text = "";
            txtTelFixe.Text = "";
            txtTelPortable.Text = "";
            txtEmail.Text = "";
            txtNom.Text = "";
            txtPrenom.Text = "";
                    
        }


        private void btnAjouter_Click(object sender, RoutedEventArgs e)
        {
            AddUser();
        }
    }
}
